package com.ztgf.user.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.DesensitizationUtils;
import com.ztgf.user.models.bo.UserInfoBO;
import com.ztgf.user.models.bo.UserInfoSubUpdateBO;
import com.ztgf.user.models.dto.UserInfoDTO;
import com.ztgf.user.models.service.IUserInfoService;
import com.ztgf.user.models.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 用户基本信息控制器
 *
 * @author zhanghualang
 * @Date 2020-05-28 17:43:46
 */

@RestController
@RequestMapping("/api/userInfo")
@Slf4j
@Api(value = "UserInfoApiController",tags = "用户基本信息Api接口")
public class UserInfoApiController {

    @DubboReference
    private IUserInfoService userInfoService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<UserInfoDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        UserInfoDTO dto = userInfoService.selectById(id);

        return new Result<>(dto);
    }

    @PostMapping("/getVo")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<UserInfoVO> getVo(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        UserInfoVO vo = userInfoService.getVo(id);

        return new Result<>(vo);
    }

    @PostMapping("/getVoByName")
    @ApiOperation(value = "根据公司名称查找",notes = "根据公司名称查找")
    @ApiImplicitParam(name = "name",value = "name",required = true,paramType = "query", dataType = "String")
    public Result<UserInfoVO> getVoByName(@RequestParam("name") String name){
        if(name == null || "".equals(name)){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        UserInfoVO vo = userInfoService.getVoByName(name);
        if (vo == null || vo.getId() == null){
            return new Result<>(CodeEnum.FAIL_Login_1001);
        }
        return new Result<>(vo);
    }

    @PostMapping("/getVoByCompanyName")
    @ApiOperation(value = "根据公司名称查找",notes = "根据公司名称查找")
    @ApiImplicitParam(name = "name",value = "name",required = true,paramType = "query", dataType = "String")
    public Result<UserInfoVO> getVoByCompanyName(@RequestParam("name") String name){
        if(name == null || "".equals(name)){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        UserInfoVO vo = userInfoService.getVoByName(name);
        if (vo == null || vo.getId() == null){
            return new Result<>(CodeEnum.FAIL_Login_1001);
        }
        //电话号码脱敏
        vo.setAddress(DesensitizationUtils.replace(vo.getAddress()));
        vo.setContactName(DesensitizationUtils.nameNum(vo.getContactName()));
        vo.setName(DesensitizationUtils.replace(vo.getName()));
        vo.setEmail(DesensitizationUtils.emailNum(vo.getEmail()));
        vo.setMobile(DesensitizationUtils.phoneNum(vo.getMobile()));
        return new Result<>(vo);
    }

    @ApiOperation(value = "修改子账号基本信息",notes = "修改子账号基本信息")
    @PostMapping("/updateSub")
    public Result update(UserInfoSubUpdateBO subUpdateBO){
        boolean flag = userInfoService.updateRole(subUpdateBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态",notes = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "status",value = "状态 0创建、1通过、2打回、3审核拒绝、4冻结、5删除",required = true,paramType = "query", dataType = "Integer")
    })
    public Result updateStatus(Long id, Integer status){
        UserInfoDTO dto = new UserInfoDTO();
        dto.setId(id);
        dto.setStatus(status);
        boolean flag = userInfoService.updateById(dto);
        if(flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @PostMapping("/updateStatusBatch")
    @ApiOperation(value = "批量修改状态",notes = "批量修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "status",value = "状态 0创建、1通过、2打回、3审核拒绝、4冻结、5删除",required = true,paramType = "query", dataType = "Integer")
    })
    public Result updateStatusBatch(String ids, Integer status){
        String[] arr = ids.split(",");
        ArrayList<UserInfoDTO> userInfoDTOS = new ArrayList<UserInfoDTO>();
        for (String id : arr){
            UserInfoDTO dto = new UserInfoDTO();
            dto.setId(Long.valueOf(id));
            dto.setStatus(status);
            userInfoDTOS.add(dto);
        }

        boolean flag = userInfoService.updateBatchById(userInfoDTOS);
        if(flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "用户基本信息列表",notes = "用户基本信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<UserInfoDTO>> queryPageList(UserInfoBO bo){
       Wrapper<UserInfoDTO> wrapper = new EntityWrapper<>();
        if (bo.getStatus() != null){
            wrapper.eq("status", bo.getStatus());
        }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());
        }
        Page<UserInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = userInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @ApiOperation(value = "激活邮箱",notes = "激活邮箱")
    @GetMapping("/activeEmail")
    public ModelAndView activeEmail(Long userId, String captcha){
        HashMap<String, Object> map = userInfoService.activeEmail(userId, captcha);
        if (map.get("tf").equals("1")){
            return  new ModelAndView(new RedirectView(map.get("url").toString()));
        }else{
            return  new ModelAndView(new RedirectView(map.get("url").toString()), map);
        }
    }
}
